<?php

class PricetypesShowsController extends AppController {

    var $name = 'PricetypesShows';

    function index() {
        $this->PricetypesShow->Pricetype->recursive = 0;
        $pricetypes = $this->PricetypesShow->Pricetype->find('all', array('conditions'=>array('Pricetype.deleted'=>0), 'order'=>array('Pricetype.name'=>'ASC')));
        $this->set('pricetypes', $pricetypes);
        $peloteros = $this->PricetypesShow->Pelotero->find('all', array('order'=>array('Pelotero.name'=>'ASC')));
        $this->set('peloteros', $peloteros);

        if (!empty($this->data)) {

            $array_datos = $this->data['PricetypesShow'];

            //empiezo una transaccion
            $this->PricetypesShow->query('SET AUTOCOMMIT = 0;');
            $this->PricetypesShow->query('START TRANSACTION;');
            $bandera = true; //la uso para ver si algo salio mal
            //borro de la tabla pricetypes_receptions
            if (!$this->PricetypesShow->deleteAll(array('1=1'), false, false)) {
                $bandera = false;
            }

            //ahora recorro los datos y voy guardando los precios de los peloteros
            foreach ($peloteros as $pelotero) {

                $array_guardar = array();
                $array_guardar['pricetype_id'] = NULL;
                $array_guardar['pelotero_id'] = $pelotero['Pelotero']['id'];
                $array_guardar['client_cost'] = $array_datos['pelotero_client_cost_' . $pelotero['Pelotero']['id']];
                $array_guardar['client_cost_extra'] = $array_datos['pelotero_client_cost_extra_' . $pelotero['Pelotero']['id']];
                $array_guardar['employee_cost'] = $array_datos['pelotero_employee_cost_' . $pelotero['Pelotero']['id']];

//                if($array_guardar['client_cost_extra'] != NULL) {
//                    pr($array_guardar);
//                    die;
//                }
                $this->PricetypesShow->create();
                if (!$this->PricetypesShow->save($array_guardar)) {
                    $bandera = false;
                }
            }
            //y ahora guardo los precios generales
            foreach($pricetypes as $pricetype) {
                $array_guardar = array();
                $array_guardar['pricetype_id'] = $pricetype['Pricetype']['id'];
                $array_guardar['pelotero_id'] = NULL;
                $array_guardar['client_cost'] = $array_datos['general_client_cost_' . $pricetype['Pricetype']['id']];
                $array_guardar['client_cost_extra'] = $array_datos['general_client_cost_extra_' . $pricetype['Pricetype']['id']];
                $array_guardar['employee_cost'] = $array_datos['general_employee_cost_' . $pricetype['Pricetype']['id']];

                $this->PricetypesShow->create();
                if (!$this->PricetypesShow->save($array_guardar)) {
                    $bandera = false;
                }
            }

            if ($bandera) {
                $this->Session->setFlash('Se ha guardado la configuración de los precios');
                $this->PricetypesShow->query('COMMIT;');
                $this->PricetypesShow->query('SET AUTOCOMMIT = 1;');
                $this->redirect(array('controller' => 'pricetypes_shows', 'action' => 'index'));
            } else {
                $this->Session->setFlash('La configuración no ha sido guardada. Por favor, intente nuevamente.');
                $this->PricetypesShow->query('ROLLBACK;');
                $this->PricetypesShow->query('SET AUTOCOMMIT = 1;');
            }
        } else {
            //si no vino nada, me cargo todo en el $this->data para que se levante solo
            $res = $this->PricetypesShow->find('all');
            foreach ($res as $tipo) {
                if ($tipo['PricetypesShow']['pelotero_id'] != NULL) {
                    $this->data['PricetypesShow']['pelotero_client_cost_' . $tipo['PricetypesShow']['pelotero_id']] = $tipo['PricetypesShow']['client_cost'];
                    $this->data['PricetypesShow']['pelotero_client_cost_extra_' . $tipo['PricetypesShow']['pelotero_id']] = $tipo['PricetypesShow']['client_cost_extra'];
                    $this->data['PricetypesShow']['pelotero_employee_cost_' . $tipo['PricetypesShow']['pelotero_id']] = $tipo['PricetypesShow']['employee_cost'];
                } else {
                    $this->data['PricetypesShow']['general_client_cost_' . $tipo['PricetypesShow']['pricetype_id']] = $tipo['PricetypesShow']['client_cost'];
                    $this->data['PricetypesShow']['general_client_cost_extra_' . $tipo['PricetypesShow']['pricetype_id']] = $tipo['PricetypesShow']['client_cost_extra'];
                    $this->data['PricetypesShow']['general_employee_cost_' . $tipo['PricetypesShow']['pricetype_id']] = $tipo['PricetypesShow']['employee_cost'];
                }
            }
        }
    }

}
